local hx__wunai = fk.CreateSkill{
  name = "hx__wunai",
  tags = { Skill.Compulsory },
}

local HX = require "packages/huixiangkill/hxfs"

Fk:addQmlMark{
  name = "wunai",
  how_to_show = function(name, value)
    if type(value) == "string" then
      return Fk:translate(value)
    elseif type(value) == "table" then
      return tostring(#value)
    end
    return " "
  end,
  qml_path = "packages/huixiangkill/qml/HXorangeBox"
}

hx__wunai:addEffect(fk.GameStart, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__wunai.name) then
      return player:getMark("@[wunai]wunai_orange") == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = #room.players - 1
    room:addPlayerMark(player, "@[wunai]wunai_orange", x)
  end,
})

hx__wunai:addEffect(fk.TurnStart, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__wunai.name) then
      return player:getMark("@[wunai]wunai_orange") == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = #room.players - 1
    room:addPlayerMark(player, "@[wunai]wunai_orange", x)
  end,
})

hx__wunai:addEffect(fk.RoundEnd, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__wunai.name) then
      return player:getMark("@[wunai]wunai_orange") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "@[wunai]wunai_orange", 1)
    if player.general == "hxdoro__shudoro" or player.deputyGeneral == "hxdoro__shudoro" then
      room:setEmotion(player, "./packages/huixiangkill/image/anim/shudoro")
    end
  end,
})

hx__wunai:addEffect(fk.AskForPeachesDone, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(hx__wunai.name) and target.hp < 1 then
      if data.who:getMark("@[wunai]wunai_orange") <= player:getMark("@[wunai]wunai_orange") and data.who ~= player then
        return player:getMark("@[wunai]wunai_orange") > 0
      elseif data.who == player then
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = table.filter(room:getOtherPlayers(player), function(p)
      return p:getMark("@[wunai]wunai_orange") > 0
    end)
    local getfrom = {}
    if data.who == player then
      if #tos > 0 then
        for _, p in ipairs(tos) do
          local choice = room:askToChoice(p, {
            choices = {"no","yes"},
            cancelable = false,
            skill_name = hx__wunai.name,
            prompt = "#hx__wunai-askfrom:"..player.id,
          })
          if choice == "yes" then
            table.insert(getfrom, p)
          end
        end
        if #getfrom > 0 then
          for _, p in ipairs(getfrom) do
            local a = p:getMark("@[wunai]wunai_orange")
            room:setPlayerMark(p, "@[wunai]wunai_orange", 0)
            room:addPlayerMark(player, "@[wunai]wunai_orange", a)
          end
          if player:getMark("@[wunai]wunai_orange") > #room.players then
            player.hp = math.abs(player.maxHp)
            room:broadcastProperty(player, "hp")
            room.logic:breakTurn()
          end
        else
          if player.general == "hxdoro__shudoro" or player.deputyGeneral == "hxdoro__shudoro" then
            room:setEmotion(player, "./packages/huixiangkill/image/anim/shudoro")
          end
        end
      else
        if player.general == "hxdoro__shudoro" or player.deputyGeneral == "hxdoro__shudoro" then
          room:setEmotion(player, "./packages/huixiangkill/image/anim/shudoro")
        end
      end
    else
      local y = math.min(10, player:getMark("@[wunai]wunai_orange") + player.hp)
      local choice = room:askToChoice(data.who, {
        choices = {"no","yes"},
        cancelable = false,
        skill_name = hx__wunai.name,
        prompt = "#hx__wunai-askto::".. player.id .. ":" .. y * 10,
      })
      if choice == "yes" then
        room:removePlayerMark(player, "@[wunai]wunai_orange", 1)
        room:addPlayerMark(data.who, "@[wunai]wunai_orange", 1)
        if HX.HasRandom(y * 10) then
          data.who.hp = math.abs(data.who.maxHp)
          room:broadcastProperty(data.who, "hp")
          if data.who.hp >= 1 then
            if HX.HasRandom(y * 7) then
              data.who:gainAnExtraTurn(false,hx__wunai.name)
            end
          else
            if player.general == "hxdoro__shudoro" or player.deputyGeneral == "hxdoro__shudoro" then
              room:setEmotion(player, "./packages/huixiangkill/image/anim/shudoro")
            end
          end
        end
      else
        if player.general == "hxdoro__shudoro" or player.deputyGeneral == "hxdoro__shudoro" then
          room:setEmotion(player, "./packages/huixiangkill/image/anim/shudoro")
        end
      end
    end
  end
})


hx__wunai:addEffect(fk.HpChanged, {
  is_delay_effect = true,
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and target:getMark("@[wunai]wunai_orange") > 0 and player:getLostHp() > 0
    and data.skillName ~= "hx__wunai"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover({
      who = player,
      num = player:getMark("@[wunai]wunai_orange"),
      skillName = hx__wunai.name,
    })
    if player.general == "hxdoro__shudoro" or player.deputyGeneral == "hxdoro__shudoro" then
      room:setEmotion(player, "./packages/huixiangkill/image/anim/shudoro")
    end
  end,
})


return hx__wunai